<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #canvas {
            background: palevioletred;
        }
    </style>
</head>
<body>
    <!-- 注意这里创建的是canvas标签 -->
    <canvas id="canvas"></canvas>
    <script>
        // 获取都画布对象
        const canvas = document.querySelector('#canvas')
        // 设置画布宽高
        canvas.height = window.innerHeight
        canvas.width = window.innerWidth
        // 获取webgl上下文
        const gl = canvas.getContext('webgl')
        // 指定清空绘图区的颜色
        gl.clearColor(0,0,0,1)
        // 使用之前指定的颜色，清空绘图区， COLOR_BUFFER_BIT是底层封装好的属性
        gl.clear(gl.COLOR_BUFFER_BIT)


        // css rgba 与webgl的颜色解析
        const cssRGBA = 'rgba(255,0,0,1)'
        const reg = RegExp(/\((.*)\)/)
        const rgbaStr = reg.exec(cssRGBA)[1]
        rgbaArr = rgbaStr.split(',').map(num => parseInt(num))
        const r = rgbaArr[0] / 255
        const g = rgbaArr[1] / 255
        const b = rgbaArr[2] / 255
        const a = rgbaArr[3] 
        gl.clearColor(r,b,g,a)
        gl.clear(gl.COLOR_BUFFER_BIT)
    </script>
</body>
</html>